#include <ti/drivers/gpio/gpio.h>
#include <ti/drivers/uart/UART.h>
#include <ti/drivers/pinmux/pinmux.h>
int main (void)
{
    //刷一下内存
    memset ((void *)L3_RAM_Buf, 0, sizeof(L3_RAM_Buf));

    int32_t         errCode;    //存放SOC初始化错误代码
    SOC_Cfg         socCfg;     //SOC配置结构体
    Task_Params     taskParams; //任务参数
    SOC_Handle      socHandle;

    ESM_init(0U);               //与安全等有关，可略过

    /* Initialize the SOC configuration: */
    /* 初始化SOC配置结构体 */
    memset ((void *)&socCfg, 0, sizeof(SOC_Cfg));
    memset((void *)&MMWave_Global_Params,0,sizeof(MMWave_Global));

    /* 配置SOC配置结构体 */
    /* Populate the SOC configuration: */
    socCfg.clockCfg = SOC_SysClock_INIT;
    socCfg.mpuCfg = SOC_MPUCfg_CONFIG;

    /* require to UNHALT the DSS if this core is available in selected device */
    socCfg.dssCfg = SOC_DSSCfg_UNHALT;

    /* Initialize the SOC Module: This is done as soon as the application is started
     * to ensure that the MPU is correctly configured. */

    /* SOC初始化，必须在系统运行后尽快调用该初始化函数 */
    socHandle = SOC_init (&socCfg, &errCode);

    if (socHandle == NULL)
    {
        System_printf ("Error: SOC Module Initialization failed [Error code %d]\n", errCode);
        return -1;
    }

    /* Wait for BSS powerup */
    if (SOC_waitBSSPowerUp(socHandle, &errCode) < 0)
    {
        /* Debug Message: */
        System_printf ("Debug: SOC_waitBSSPowerUp failed with Error [%d]\n", errCode);
        return 0;
    }

    /* Check if the SOC is a secure device */
    if (SOC_isSecureDevice(socHandle, &errCode))
    {
        /* Disable firewall for JTAG and LOGGER (UART) which is needed by all unit tests */
        SOC_controlSecureFirewall(socHandle,
                                  (uint32_t)(SOC_SECURE_FIREWALL_JTAG | SOC_SECURE_FIREWALL_LOGGER),
                                  SOC_SECURE_FIREWALL_DISABLE,
                                  &errCode);
    }

    MMWave_Global_Params.handle.socHandle = socHandle;

    /* Debug Message: */
    System_printf ("Debug: Launching the MMW HWA Demo\n");

    /* Initialize the Task Parameters. */
    /* 创建一个任务用于测试 */
    Task_Params_init(&taskParams);
    //taskParams.priority = 3;      //优先级3
    MMWave_Global_Params.task.init = Task_create(MMWave_InitTask, &taskParams, NULL);
    /* Start BIOS */
    BIOS_start();
    return 0;
}

 GPIO_init();                //GPIO初始化
 UART_init();                //串口初始化
void Init_Pinmux(void)
{
    /* 串口引脚初始化 */
    /* 共有两个串口，分别为UART-1和UART-3，序号为0和2 */
    /* Setup the PINMUX to bring out the MSS UART-1 */

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINN5_PADBE, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINN5_PADBE, SOC_XWR68XX_PINN5_PADBE_MSS_UARTA_TX);
    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINN4_PADBD, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINN4_PADBD, SOC_XWR68XX_PINN4_PADBD_MSS_UARTA_RX);

    // 这一块用不上
    /* Setup the PINMUX to bring out the MSS UART-3 */
//    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINF14_PADAJ, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
//    Pinmux_Set_FuncSel(SOC_XWR68XX_PINF14_PADAJ, SOC_XWR68XX_PINF14_PADAJ_MSS_UARTB_TX);
	
	/**********************************************************************
     * Setup the PINMUX:
     * - GPIO Output: Configure pin K13 as GPIO_2 output
     **********************************************************************/
    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINK13_PADAZ, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINK13_PADAZ, SOC_XWR68XX_PINK13_PADAZ_GPIO_2);

    /
    **********************************************************************
     * Setup the PINMUX:
     * - for QSPI Flash
     **********************************************************************/
    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINR12_PADAP, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINR12_PADAP, SOC_XWR68XX_PINR12_PADAP_QSPI_CLK);

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINP11_PADAQ, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINP11_PADAQ, SOC_XWR68XX_PINP11_PADAQ_QSPI_CSN);

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINR13_PADAL, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINR13_PADAL, SOC_XWR68XX_PINR13_PADAL_QSPI_D0);

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINN12_PADAM, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINN12_PADAM, SOC_XWR68XX_PINN12_PADAM_QSPI_D1);

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINR14_PADAN, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINR14_PADAN, SOC_XWR68XX_PINR14_PADAN_QSPI_D2);

    Pinmux_Set_OverrideCtrl(SOC_XWR68XX_PINP12_PADAO, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR68XX_PINP12_PADAO, SOC_XWR68XX_PINP12_PADAO_QSPI_D3);
	/**********************************************************************
     * Setup the GPIO:
     * - GPIO Output: Configure pin K13 as GPIO_2 output
     **********************************************************************/
    /**********************************************************************
     * Setup the DS3 LED on the EVM connected to GPIO_2
     **********************************************************************/
    GPIO_setConfig (SOC_XWR68XX_GPIO_2, GPIO_CFG_OUTPUT);
}

void Init_UART(void)
{
    UART_Params uart0_params;
    UART_Params_init(&uart0_params);

    uart0_params.baudRate = 115200;                   //波特率115200*8
    uart0_params.clockFrequency = MSS_SYS_VCLK;      //系统时钟频率200MHz
    uart0_params.dataLength = UART_LEN_8;             //8bit字长
    uart0_params.isPinMuxDone = 1;                    //已经完成引脚复用初始化
    uart0_params.parityType = UART_PAR_NONE;          //无校验位
    uart0_params.readDataMode = UART_DATA_BINARY;     //按位方式读取数据
    uart0_params.readEcho = UART_ECHO_OFF;            //关闭回显
    uart0_params.readReturnMode = UART_RETURN_FULL;   //完整读取
    uart0_params.stopBits = UART_STOP_ONE;            //1停止位
    uart0_params.writeDataMode = UART_DATA_BINARY;    //按位方式写入（发送）数据

    MMWave_Global_Params.handle.uart0_handle = UART_open(0, &uart0_params);             //打开串口

    if(MMWave_Global_Params.handle.uart0_handle == NULL)
    {
        System_printf("UART0 open fail\n!");
        return;
    }

    System_printf("UART0 open success\n!");
    uint8_t buf[]="[INFO] UART0 Test\n";

    UART_write(MMWave_Global_Params.handle.uart0_handle, buf, sizeof(buf));
}


